/*
# File    :   spi.c
# Author  :   SCHIPS
# Mail    :   schips@dingtalk.com
# Date    :   Tue, Apr 30, 2019  4:04:21 PM
# Note    :   SPI with R/W SD card demo based on C51
*/

#include <reg52.h>
#include <intrins.h>
#include <stdio.h>
#include <9325TP.h>
//=============================================================
//定义SD卡需要的4根信号线
sbit SPI_CLK = P1^1;
sbit SPI_DI  = P1^2;
sbit SPI_DO  = P1^0;
sbit SPI_CS  = P1^3;
//===========================================================
//定义按键端口
sbit KEY = P3^2;
//===========================================================
//定义512字节缓冲区，注意需要使用 xdata关键字
unsigned char xdata DATA[75]={0};
unsigned char xdata DATA1[75]={0};

//写一字节, 模拟SPI总线方式
void SPIWrite(unsigned char n)
{
    unsigned char i;

    for(i = 8; i ; i--)
    {
        SPI_CLK=0;
        SPI_DI=(n&0x80);
        n<<=1;
        SPI_CLK=1;
    }
    SPI_DI=1;
}
//读一字节, 模拟SPI总线方式
unsigned char SPIRead()
{
    unsigned char n,i;
    for(i = 8; i ; i--)
    {
        SPI_CLK = 0;
        SPI_CLK = 1;
        n <<= 1;
        if(SPI_DO) 
        {
            n |= 1;
        }
    }
    return n;
}
